﻿#If NETFX_CORE Then

Imports Microsoft.Xna.Framework
Imports Windows.ApplicationModel.Core

#If FEATURE_OWN_MONO_GAME Then

Namespace Global.Runa.Common.MetroGame

    Public Class MetroFrameworkView(Of T As Game)
        Implements IFrameworkView

        Public Property ApplicationView() As CoreApplicationView
        Public Property Game() As T

        Public Sub New()
            Me.Game = Nothing
        End Sub

        Public Sub New(ByVal g As T)
            Me.Game = g
        End Sub

        Public Sub Initialize(ByVal applicationView As CoreApplicationView) Implements IFrameworkView.Initialize
            Me.ApplicationView = applicationView
            AddHandler Me.ApplicationView.Activated, AddressOf ViewActivated
        End Sub

        Public Sub Load(ByVal entryPoint As String) Implements IFrameworkView.Load
        End Sub

        Public Sub Run() Implements IFrameworkView.Run
            Me.Game.Run()
        End Sub

        Public Sub SetWindow(ByVal window As Windows.UI.Core.CoreWindow) Implements IFrameworkView.SetWindow
            MetroGameWindow.Instance.Initialize(window, Nothing)
        End Sub

        Public Sub Uninitialize() Implements IFrameworkView.Uninitialize
            Me.Game.Dispose()
        End Sub

        Private Sub ViewActivated(ByVal sender As CoreApplicationView, ByVal args As IActivatedEventArgs)
            If (args.Kind = ActivationKind.Launch) Then
                MetroGamePlatform.LaunchParameters = DirectCast(args, LaunchActivatedEventArgs).Arguments
                MetroGamePlatform.PreviousExecutionState = DirectCast(args, LaunchActivatedEventArgs).PreviousExecutionState
                Me.Game = If(Me.Game, Activator.CreateInstance(Of T)())
            ElseIf (args.Kind = ActivationKind.Protocol) Then
                Dim protocolArgs As ProtocolActivatedEventArgs = TryCast(args, ProtocolActivatedEventArgs)
                MetroGamePlatform.LaunchParameters = protocolArgs.Uri.AbsoluteUri
                MetroGamePlatform.PreviousExecutionState = protocolArgs.PreviousExecutionState
                Me.Game = If(Me.Game, Activator.CreateInstance(Of T)())
            End If
        End Sub

    End Class

End Namespace

#End If

#End If